import java.util.Arrays;

public class SelectSortOP {
    public static void selectSortOP(int[] array){
        int low = 0;
        int high = array.length - 1;
        // [low,high] 表示整个无序区间
        while(low < high){
            int min = low;
            int max = low;
            for (int i = low+1; i <= high; i++) {
                if(array[i] < array[min]){
                    min = i;
                }
                if(array[i] > array[max]){
                    max = i;
                }
            }
            swap(array,min,low);
            if(max == low){
                max = min;
            }
            swap(array,max,high);
            low++;
            high--;
        }

    }
    public static void swap(int[] array,int x,int y){
        int tmp = array[x];
        array[x] = array[y];
        array[y] = tmp;
    }

    public static void main(String[] args) {
        int[] array = {9, 5, 2, 7, 3, 6, 8 };
        selectSortOP(array);
        System.out.println(Arrays.toString(array));
    }
}
